package com.atguigu.dga.governance.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.hadoop.hive.ql.parse.SemanticException;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.format.DateTimeParseException;
import java.util.Date;

/**
 * ClassName: Assessor
 * Package: com.atguigu.dga.governance.assessor
 * Description:
 *
 * @Author:
 * @Create: 2024/3/16 15:16
 * @Version: 1.0
 */
public abstract class Assessor {
    /**
     * 模板设计模式：
     *
     * 父类把控考评这个流程: 子类实现查找问题的细节
     * 每个考评器考评流程一样，每个考评器查找问题的细节不一样
     */
    public final GovernanceAssessDetail doAssess(AssessParam assessParam) {
        System.out.println("表：" + assessParam.getTableMetaInfo().getTableName() + ", 指标："
                + assessParam.getGovernanceMetric().getMetricName() + "开始考评");

        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        // 定义流程
        // TODO 1 给考评结果 通用的信息赋值、其他等checkProblem
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setSchemaName(assessParam.getTableMetaInfo().getSchemaName());
        governanceAssessDetail.setTableName(assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setMetricId(assessParam.getGovernanceMetric().getId().toString());
        governanceAssessDetail.setMetricName(assessParam.getGovernanceMetric().getMetricName());
        governanceAssessDetail.setGovernanceType(assessParam.getGovernanceMetric().getGovernanceType());
        governanceAssessDetail.setTecOwner(assessParam.getTableMetaInfo().getTableMetaInfoExtra().getTecOwnerUserName());
        // 分数 先给满分 checkProblem中再重新给
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);




        // TODO 2 查找问题
        try {
            checkProblem(assessParam, governanceAssessDetail);
        } catch (Exception e) {
            e.printStackTrace();
            // 考评中出现异常
            governanceAssessDetail.setIsAssessException("1");

            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);

            governanceAssessDetail.setAssessExceptionMsg(
                    stringWriter.toString().
                            substring(0, Math.min(2000, stringWriter.toString().length())));
        }

        // 根据分数判断 是否给定治理链接
        String governanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl();
        if(governanceAssessDetail.getAssessScore().longValue() < 10L
                && governanceUrl != null) {
            governanceUrl = governanceUrl.replace("{tableId}", assessParam.getTableMetaInfo().getId().toString());
            governanceAssessDetail.setGovernanceUrl(governanceUrl);
        }

        governanceAssessDetail.setCreateTime(new Date());



        // 考评结果
        return governanceAssessDetail;
    }


    public abstract void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException, URISyntaxException, IOException, InterruptedException, org.apache.hadoop.hive.ql.parse.ParseException, SemanticException;

//    public abstract void doAssess();
}
